package main

import (
	"fmt"
	"regexp"
)

func main() {
	// 匹配是否存在
	match, err := regexp.Match("\\d+\\.\\d+\\.\\d+\\.\\d+", []byte("ip = 192.168.1.1"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(match)
	match, err = regexp.Match("\\d+\\.\\d+\\.\\d+\\.\\d+", []byte("ip = 192.168.1"))
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(match)
	match, err = regexp.MatchString("\\d+\\.\\d+\\.\\d+\\.\\d+", "ip = 192.168.1.1")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(match)

	// 输出全部匹配的内容
	reg := regexp.MustCompile("\\d+\\.\\d+\\.\\d+\\.\\d+")
	r := reg.FindString("ip1 = 192.168.1.1, ip2 = 192.168.1.2")
	fmt.Println(r)
	rs := reg.FindAllString("ip1 = 192.168.1.1, ip2 = 192.168.1.2", -1)
	fmt.Println(rs)
	is := reg.FindAllStringIndex("ip1 = 192.168.1.1, ip2 = 192.168.1.2", -1)
	fmt.Println(is)

	// 输出匹配的子字符串内容
	reg1 := regexp.MustCompile("ip\\d*\\s*=\\s*([^\\s,]*)\\s*,?")
	rss := reg1.FindAllStringSubmatch("ip1 = 192.168.1.1, ip2 = 192.168.1.2", -1)
	fmt.Println(rss)
	iss := reg1.FindAllStringSubmatchIndex("ip1 = 192.168.1.1, ip2 = 192.168.1.2", -1)
	fmt.Println(iss)

	// 替换匹配的内容
	r = reg.ReplaceAllString("ip1 = 192.168.1.1, ip2 = 192.168.1.2", "0.0.0.0")
	fmt.Println(r)
}

